package com.jxwifi.cloud.quickclean.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxwifi.cloud.quickclean.R;


/**
 * @author Kingyo
 */
public class CancelReasonDialog {

    private String TAG = this.getClass().getSimpleName();
    private TextView mTvTitle;
    private EditText mTvContent;
    private TextView mTvLeftBtn;
    private TextView mTvRightBtn;
    public TextView tv_wait_long;
    public TextView tv_time_wrong;
    private TextView tv_complaint_num;

    private LinearLayout lin_wait_long;
    private LinearLayout lin_time_wrong;
    private Dialog mDialog;
    private Context mContext;
    public String cancelReason="";

    public CancelReasonDialog(Context context) {
        this(context, "");
    }

    public CancelReasonDialog(Context context, String content) {
        if (mDialog == null) {
            mContext = context;
            mDialog = new Dialog(context, com.idroid.R.style.loading_dialog);
            View root = View.inflate(context, R.layout.dialog_cancel_reason, null);
            mTvTitle = (TextView) root.findViewById(R.id.tv_dialog_title);
            mTvContent = (EditText) root.findViewById(R.id.tv_dialog_content);
            mTvLeftBtn = (TextView) root.findViewById(R.id.tv_dialog_leftbtn);
            mTvRightBtn = (TextView) root.findViewById(R.id.tv_dialog_rightbtn);
            tv_wait_long = (TextView) root.findViewById(R.id.tv_wait_long);
            tv_time_wrong = (TextView) root.findViewById(R.id.tv_time_wrong);
            tv_complaint_num = (TextView) root.findViewById(R.id.tv_complaint_num);
            lin_wait_long = (LinearLayout) root.findViewById(R.id.lin_wait_long);
            lin_time_wrong = (LinearLayout) root.findViewById(R.id.lin_time_wrong);
            //lin_wait_long.setBackground(mContext.getResources().getDrawable(R.drawable.shap_bottom_buttom_selector));
            mDialog.setContentView(root);
        }
        if (!TextUtils.isEmpty(content)) mTvContent.setText(content);
        mTvLeftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mTvRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mTvContent.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus==true){//获取焦点了
                    mTvContent.setHint("");
                }
            }
        });
        lin_wait_long.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        lin_time_wrong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }

    public CancelReasonDialog setContentText(String txt) {
        mTvContent.setText(txt);
        return this;
    }

    public String getContentText() {

        return mTvContent.getText().toString().trim();
    }

    public CancelReasonDialog setTitleText(String txt) {
        mTvTitle.setVisibility(View.VISIBLE);
        mTvTitle.setText(txt);
        return this;
    }

    /**
     * 点击外部不消失
     * @param cancelable
     */
    public void setCancelable(boolean cancelable) {
        mDialog.setCancelable(cancelable);
    }

    /**
     * 设置左边btn的文本、点击事件。不需要设置就传null
     */
    public CancelReasonDialog setLeftTxt(String txt, View.OnClickListener listener) {
        if (null != txt) {
            mTvLeftBtn.setText(txt);
        }
        if (null != listener) {
            mTvLeftBtn.setOnClickListener(listener);
        }
        dismiss();
        return this;
    }

    public CancelReasonDialog leftTxtGone(){
        mTvLeftBtn.setVisibility(View.GONE);
        return this;
    }
    public CancelReasonDialog rightTxtGone(){
        mTvRightBtn.setVisibility(View.GONE);
        return this;
    }

    /**
     * 设置右边btn的文本、点击事件。不需要设置就传null
     */
    public CancelReasonDialog setRightTxt(String txt, View.OnClickListener listener) {
        if (null != txt) {
            mTvRightBtn.setText(txt);
        }
        if (null != listener) {
            mTvRightBtn.setOnClickListener(listener);
        }
        dismiss();
        return this;
    }
    /**
     * 设置其他原因点击事件。不需要设置就传null
     */
    public CancelReasonDialog setOther(View.OnFocusChangeListener listener) {
        if (null != listener) {
            mTvContent.setOnFocusChangeListener(listener);
        }
        return this;
    }

    public CancelReasonDialog addTextChanged() {
        mTvContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tv_complaint_num.setText(s.length()+"/10");
                cancelReason = s.toString().trim();
                if (s.length()>0){
                    tv_wait_long.setBackgroundColor(Color.parseColor("#FFFFFFFF"));
                    tv_time_wrong.setBackgroundColor(Color.parseColor("#FFFFFFFF"));
                }
                Log.i(TAG,"otherChecked ==> cancelReason = "+cancelReason);
            }
        });
        return this;
    }

    public CancelReasonDialog otherChecked(boolean isFocus){
        if(isFocus){
            cancelReason = mTvContent.getText().toString();
            tv_wait_long.setBackgroundColor(Color.parseColor("#FFFFFFFF"));
            tv_time_wrong.setBackgroundColor(Color.parseColor("#FFFFFFFF"));
            Log.i(TAG,"otherChecked ==> cancelReason = "+cancelReason+" isFocus = "+isFocus);
        }

        return this;
    }
    /**
     * 设置等待太久点击事件。不需要设置就传null
     */
    public CancelReasonDialog setWaitLong(View.OnClickListener listener) {
        if (null != listener) {
            lin_wait_long.setOnClickListener(listener);
        }
        return this;
    }

    public CancelReasonDialog waitLongChecked(){
        tv_wait_long.setBackgroundResource(R.drawable.shap_bottom_buttom_selector);
        tv_time_wrong.setBackgroundColor(Color.parseColor("#FFFFFFFF"));
        cancelReason = tv_wait_long.getText().toString();
        //mTvContent.clearFocus();//影响上面三行效果
        //mTvContent.setFocusableInTouchMode(false);//高版本无法弹出键盘
        //mTvContent.setFocusableInTouchMode(true);
        Log.i(TAG,"waitLongChecked ==> cancelReason = "+cancelReason);

        return this;
    }

    /**
     * 设置选错时间点击事件。不需要设置就传null
     */
    public CancelReasonDialog setTimeWrong(View.OnClickListener listener) {
        if (null != listener) {
            lin_time_wrong.setOnClickListener(listener);

        }
        return this;
    }
    public CancelReasonDialog timeWrongChecked(){
        tv_time_wrong.setBackgroundResource(R.drawable.shap_bottom_buttom_selector);
        tv_wait_long.setBackgroundColor(Color.parseColor("#FFFFFFFF"));
        cancelReason = tv_time_wrong.getText().toString();
        //mTvContent.setFocusableInTouchMode(false);
        //mTvContent.clearFocus();//影响上面三行效果
        //mTvContent.setFocusableInTouchMode(true);
        Log.i(TAG,"timeWrongChecked ==> cancelReason = "+cancelReason);
        return this;
    }

    public void show() {
        if (mDialog != null) {
            mDialog.show();
            mDialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        }
    }

    public void dismiss() {
        if (mDialog != null)
            mDialog.dismiss();
    }

    public boolean isShowing() {
        return mDialog.isShowing();
    }

}
